package com.java.multipledatasource.spring.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author hu
 * @version 1.0
 * @date 2020/12/20 0020 下午 8:25
 * @description
 */
public class Test01 {
    static  int val = 0;
    static ReentrantLock lock = new ReentrantLock(true);

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> count(), "t1");
        Thread t2 = new Thread(() -> count(), "t2");
        Thread t3 = new Thread(() -> count(), "t3");

        t1.start();
        t2.start();
        t3.start();


        System.out.println(val);
    }

    static  void count() {
        lock.lock();
        try {
            for (int i = 0; i < 1000; i++) {
                val++;
            }
        }finally {
            lock.unlock();
        }


    }
}
